Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongodb

Package Overview
Dependencies
Maintainers
8
Versions
574
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb

The official MongoDB driver for Node.js

  • 6.10.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.3M
decreased by-63.04%
Maintainers
8
Weekly downloads
 
Created

What is mongodb?

The mongodb npm package is the official Node.js driver for MongoDB. It provides a high-level API to connect to and interact with MongoDB databases. With this package, developers can perform CRUD operations, manage database connections, and work with MongoDB features like transactions, indexes, and aggregation.

What are mongodb's main functionalities?

Connecting to a MongoDB database

This code sample demonstrates how to connect to a MongoDB database using the MongoClient object provided by the mongodb package.

const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
async function connect() {
  try {
    await client.connect();
    console.log('Connected to MongoDB');
  } catch (e) {
    console.error(e);
  }
}
connect();

CRUD Operations

This code sample shows how to perform CRUD (Create, Read, Update, Delete) operations on a MongoDB collection using the mongodb package.

const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'myDatabase';
async function crudOperations() {
  try {
    await client.connect();
    const db = client.db(dbName);
    const collection = db.collection('documents');

    // Create a document
    await collection.insertOne({ a: 1 });

    // Read documents
    const docs = await collection.find({}).toArray();

    // Update a document
    await collection.updateOne({ a: 1 }, { $set: { b: 1 } });

    // Delete a document
    await collection.deleteOne({ b: 1 });
  } catch (e) {
    console.error(e);
  } finally {
    await client.close();
  }
}
crudOperations();

Index Management

This code sample illustrates how to manage indexes in a MongoDB collection, including creating an index and listing all indexes.

const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'myDatabase';
async function manageIndexes() {
  try {
    await client.connect();
    const db = client.db(dbName);
    const collection = db.collection('documents');

    // Create an index
    await collection.createIndex({ a: 1 });

    // List indexes
    const indexes = await collection.indexes();
    console.log(indexes);
  } catch (e) {
    console.error(e);
  } finally {
    await client.close();
  }
}
manageIndexes();

Aggregation

This code sample demonstrates how to use the aggregation framework provided by MongoDB to process data and compute aggregated results.

const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'myDatabase';
async function aggregateData() {
  try {
    await client.connect();
    const db = client.db(dbName);
    const collection = db.collection('documents');

    // Perform an aggregation query
    const aggregation = await collection.aggregate([
      { $match: { a: 1 } },
      { $group: { _id: '$b', total: { $sum: 1 } } }
    ]).toArray();
    console.log(aggregation);
  } catch (e) {
    console.error(e);
  } finally {
    await client.close();
  }
}
aggregateData();

Other packages similar to mongodb

Keywords

FAQs

Package last updated on 21 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc